package com.ror.java.se.file;

//删除一个多级文件夹

import java.io.File;
import java.util.Scanner;

public class Demo06 {
    public static void main(String[] args) {
        File workDir = new File("assets", "file");
        //先创建一个多级文件夹
        File dir = workDir;
        for (String childDirName : new String[]{"a", "b", "c"}) {
            dir = new File(dir, childDirName);
            dir.mkdirs();
        }
        System.out.print("多级目录已创建，按回车键开始删除……");
        Scanner sc = new Scanner(System.in);
        sc.nextLine();
        sc.close();

        //递归删除多级文件夹
        delete(new File(workDir, "a"));
    }

    static void delete(File parent) {
        if (!parent.exists()) return;
        File[] children = parent.listFiles();
        if (children == null) return;
        for (File child : children) {
            if (!child.delete()) {
                delete(child);
                child.delete();
            }
        }
        parent.delete();
    }
}
